/**
 * Created by zhanglihe on 15/12/19.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'template',
    'model',
    'dialog'
], function( $, common, ready, template, Model, dialog ) {

    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            this.event();
        },

        event: function() {
            var _this = this;
            $('#btn-save-profile').on('click', function(e) {
                e.preventDefault();
                _this.saveProfile();
            });
            $('#btn-save-pwd').on('click', function(e) {
                e.preventDefault();
                _this.savePwd();
            });
        },
        saveProfile: function() {
            var model = new Model({
                url: _global.url.api + 'admin/profile',
                data: {
                    real_name: $('input[name="real_name"]').val()
                },
                valid_config: {
                    real_name : 'required'
                }
            });
            model.put();
        },
        savePwd: function() {
            var model = new Model({
                url: _global.url.api + 'admin/profile',
                data: {
                    old_password: $('input[name="old_password"]').val(),
                    new_password: $('input[name="new_password"]').val(),
                    confirm_password: $('input[name="confirm_password"]').val()
                },
                valid_config: {
                    old_password : 'required',
                    new_password : 'required',
                    confirm_password : 'required'
                },
                success: function() {
                    //修改密码后，重新登录
                    dialog({
                        content: '密码修改成功，请牢记新密码，点击确认立即重新登录。',
                        ok: function() {
                            window.location.href = '/logout?m=agency';
                        }
                    }).show();
                }
            });
            if(model.data.new_password != model.data.confirm_password) {
                Alert({msg: '两次输入的密码不一致', type: 'error'});
                return false;
            }
            model.put();
        }
    };

    new index();

});